package com.yayhos.parallaxrecyclerview.sample.slice;

import com.yayhos.parallaxrecyclerview.ParallaxViewHolder;
import com.yayhos.parallaxrecyclerview.sample.ResourceTable;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import java.io.IOException;
import java.io.InputStream;

public class RecyclerAdapter extends BaseItemProvider {
    private Context mContext;
    private final int[] imageIds = new int[]{ResourceTable.Media_test_image_1,
            ResourceTable.Media_test_image_2, ResourceTable.Media_test_image_3,
            ResourceTable.Media_test_image_4, ResourceTable.Media_test_image_5};
    private static final String FORMAT = "image/jpg";

    private PixelMap[] pixelMaps;


    private PixelMap getPixelMapFromResource(Context context, int resId, ImageSource.DecodingOptions decodingOptions) {
        PixelMap pixelmap;
        try (InputStream drawableInputStream = context.getResourceManager().getResource(resId)) {
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = FORMAT;
            ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions);
            pixelmap = imageSource.createPixelmap(decodingOptions);
            return pixelmap;
        } catch (IOException | NotExistException e) {
            e.getMessage();
        }
        return null;
    }

    public RecyclerAdapter(Context context) {

        this.mContext = context;

        pixelMaps = new PixelMap[imageIds.length];

        for (int i = 0; i < imageIds.length; i++) {

            pixelMaps[i] = getPixelMapFromResource(context, imageIds[i], new ImageSource.DecodingOptions());

        }

    }

    @Override
    public int getCount() {
        return 50;
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        ViewHolder holder;

        if (component == null) {
            cpt = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_ability_item, null, false);
            holder = new ViewHolder(cpt);
            cpt.setTag(holder);


        } else {
            cpt = component;
            holder = (ViewHolder) cpt.getTag();
        }
        holder.getTextView().setText("ROW " + position);
        ShapeElement background = new ShapeElement();
        background.setRgbColor(new RgbColor(Color.WHITE.getValue()));
        holder.getBackgroundImage().setBackground(background);
        holder.getBackgroundImage().setMyPixelMap(pixelMaps[position % imageIds.length]);
        return cpt;
    }

    public static class ViewHolder extends ParallaxViewHolder {

        private final Text textView;
        public ViewHolder(Component v) {
            super(v);
            textView = (Text) v.findComponentById(ResourceTable.Id_title);
        }

        @Override
        public int getParallaxImageId() {
            return ResourceTable.Id_backgroundImage;
        }

        public Text getTextView() {
            return textView;
        }
    }

}
